/*
 * @Author: taoyuqing
 * @Date: 2023-03-19 18:04:55
 * @Description: 
 */
import 'package:flutter/material.dart';

class HistoryTable extends StatefulWidget {
  late final List<Map> data;
  final List<String> header;
  final List<String> dataKeys;
  final String? firstKey;
  final double dataLength;
  late final double? cellWidth;

  HistoryTable(
      {Key? key,
      required this.data,
      this.firstKey,
      required this.header,
      required this.dataLength,
      required this.dataKeys,
      this.cellWidth})
      : super(key: key);

  @override
  State<HistoryTable> createState() => _HistoryTableState();
}

class _HistoryTableState extends State<HistoryTable> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          widget.firstKey != null
              ? Container(
                  width: 150,
                  child: Table(children: _createFirstColumData()),
                )
              : Container(),
          Expanded(
            child: SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Container(
                child: Table(
                  defaultColumnWidth: const FixedColumnWidth(50),
                  children: _createTableDataWithHeader(),
                ),
                width: (widget.cellWidth ?? 100) * widget.dataLength,
              ),
            ),
          )
        ],
      ),
    );
  }

  List<TableRow> _createTableDataWithHeader() {
    TableRow header = _createTableHeader(widget.header);
    List<TableRow> tableRows = [];

    List<TableRow> datalist = widget.data.map((e) {
      Map data = {};
      widget.dataKeys.forEach((element) {
        data[element] = e[element];
      });
      List content = [];
      data.forEach((key, value) {
        content.add(value);
      });
      return TableRow(children: _createTableData(content));
    }).toList();
    return [header, ...datalist];
  }

  TableRow _createTableHeader(List data) {
    if (widget.firstKey == null) {
      List<Widget> list = data.map((e) {
        return Container(
          alignment: Alignment.center,
          height: 40,
          child: Text('$e', style: TextStyle(color: Colors.white)),
        );
      }).toList();
      return TableRow(children: list);
    }
    List header = data;
    header.removeAt(0);
    List<Widget> list = header.map((e) {
      return Container(
        alignment: Alignment.center,
        height: 40,
        child: Text('$e', style: TextStyle(color: Colors.white)),
      );
    }).toList();
    return TableRow(children: list);
  }

  List<Widget> _createTableData(
    List data,
  ) {
    if (widget.firstKey == null) {
      return data.map((e) {
        return Container(
          alignment: Alignment.center,
          height: 40,
          child: Text('${e}', style: TextStyle(color: Colors.white)),
        );
      }).toList();
    }
    List header = data;
    header.removeAt(0);
    return header.map((e) {
      return Container(
        alignment: Alignment.center,
        height: 40,
        child: Text('${e}', style: TextStyle(color: Colors.white)),
      );
    }).toList();
  }

  _createFirstColumData() {
    String key = widget.dataKeys[0];
    List<TableRow> list = widget.data.map((e) {
      return TableRow(children: [
        Container(
          alignment: Alignment.center,
          height: 40,
          child: Text('${e[key]}', style: TextStyle(color: Colors.white)),
        )
      ]);
    }).toList();
    return [
      TableRow(children: [
        Container(
          alignment: Alignment.center,
          height: 40,
          child: Text(
            '${widget.header[0]}',
            style: TextStyle(color: Colors.white),
          ),
        ),
      ]),
      ...list
    ];
  }
}
